package com.gtm.service;

import com.gtm.model.Review;
import com.gtm.model.Topic;
import com.gtm.model.Translation;
import com.gtm.model.User;
import com.gtm.repository.TopicRepository;
import com.gtm.repository.TranslationRepository;
import com.gtm.repository.UserRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TranslationService extends BaseService<Translation>{
    @Autowired
    MongoTemplate mt;

    @Autowired
    private TranslationRepository tlr;

    @Autowired
    UserRepository usr;

    @Autowired
    TopicRepository tpr;

    public Translation uploadTranslation(String sid, String summary, MultipartFile reviewFile) throws IOException {
        User user = usr.findOne(sid);
        if(user.getAdditionalData()==null||user.getAdditionalData().get("state")==null)
            return null;
        String state = user.getAdditionalData().get("state").toString();
        List<String> stlist=new ArrayList<String>();
        stlist.add("REVIEW_PASS");stlist.add("TRANSLATION_PENDING");
        if(stlist.contains(state)==false)
            return null;

        Translation tl=null;
        if(tlr.findByStuId(sid)!=null) {
            tl = tlr.findByStuId(sid);
        }
        else {
            tl=new Translation();
            tl.setStuId(sid);
        }

        String tid=user.getTopicId();
        String sname=user.getRealname();
        tl.setStuName(sname);
        tl.setTopicId(tid);
        tl.setSummary(summary);
        String refile=upUserFile(reviewFile,tl);
        DBObject ref=new BasicDBObject();
        ref.put("translationDoc",refile);
        ref.put("uploadTime",new Date());
        List<DBObject> relist=new ArrayList<DBObject>();
        if(null!= tl.getTransFile())
            relist= tl.getTransFile();
        relist.add(ref);
        tl.setTransFile(relist);
        Topic tpc=tpr.findOne(tid);
        String topicNmae=tpc.getTopicName();
        String teacherName=tpc.getTeacherName();
        String teacherId=tpc.getTeacherId();
        tl.setTopicName(topicNmae);
        tl.setTeacherId(teacherId);
        tl.setTeacherName(teacherName);
        user.setAdditionalData("state","TRANSLATION_PENDING");
        user.setAdditionalData("translationId",tlr.save(tl).getId());
        usr.save(user);
        return null;

    }

    public Translation passTranslation(String rid, String comment) {
        Translation tl=new Translation();
        tl=tlr.findOne(rid);
        tl.setState("PASS");
        tl.setComment(comment);
        String sid=tl.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","TRANSLATION_PASS");
        usr.save(user);
        return tlr.save(tl);
    }

    public Translation depassTranslation(String rid, String comment) {
        Translation tl=new Translation();
        tl=tlr.findOne(rid);
        tl.setState("PENDING");
        tl.setComment(comment);
        String sid=tl.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","TRANSLATION_PENDING");
        usr.save(user);
        return tlr.save(tl);
    }
}
